﻿/////////////////////////////////////////////////////////////////////
//
//	Map_FVG C# Class Library.
//
//	infoDialog4
//	
//
//	autore: Gustavo Mainardi 
//	versione: 1.0
//	data: 01/12/2015
//
//
//	Cronologia delle versioni:
//
//	Versione 1.0 01/12/2015
//		versione originale
//
//////////////////////////////////////////////////////////////////////////
using System;
using System.Drawing;
using System.Windows.Forms;
using System.Data.SQLite;

namespace Map_Fvg
{
    public partial class infoDialog4 : Form
    {
        public infoDialog4()
        {
            InitializeComponent();
            using (SQLiteConnection conn = SetConnection())
            {
                conn.Open();
                using( SQLiteCommand cmd = new SQLiteCommand(conn))
                {
                    cmd.CommandText = "select * from classi";
                    
                    SQLiteDataReader reader = cmd.ExecuteReader();
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            comboBox1.Items.Add(reader.GetString(0).ToString() + " - " + reader.GetString(1).ToString());
      //                      listBox1.Items.Add(reader.GetString(0).ToString() + " - " +reader.GetString(1).ToString());
                        }
                    }
                conn.Close();
                }
                comboBox1.SelectedIndex = 0;
                textBox1.Text = "1L"; 
            }
            /*
            catch (SQLiteException ex)
            {
                MessageBox.Show("errore specifico SQLite " + ex.ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show("errore generico " + ex.ToString());
            }
            finally
            {
                conn.Close();
            }
             * */
        }

        private SQLiteConnection SetConnection()
        {
            string DataSource = "Data Source=";
            DataSource = string.Concat(DataSource, FormIniz.cartellaSimb, ";");
            SQLiteConnection conn = new SQLiteConnection(DataSource);
            return (conn);
        }


        private void button1_Click(object sender, EventArgs e)
        {
            string s1 = textBox1.Text.Trim();
            s1 = s1.ToUpper();
            int length = s1.Length;
            if (length < 7)
            {
                MessageBox.Show("completare il codice sottoclasse");
                return;
            }

            string s5 = textBox4.Text;
            if (s5.Length == 0)
            {
                MessageBox.Show("manca denominazione linea");
                return;
            }

            string s6 = textBox5.Text;
            if (s6.Length == 0)
            {
                MessageBox.Show("indicare tipo linea");
                return;
            }

            string s3 = textBox2.Text;
            if(s3.Length == 0) 
            {
                MessageBox.Show("manca descrizione tratto");
                return;
            }

            Color icol = textBox3.BackColor;

            string ss = icol.A.ToString();

            ss = string.Concat(ss, " ", icol.R.ToString(), " ", icol.G.ToString(), " ", icol.B.ToString());

            using (SQLiteConnection conn = SetConnection())
            {
                conn.Open();
                using (SQLiteCommand cmd = new SQLiteCommand(conn))
                {
                    cmd.CommandText = "select * from linea where codice_stclasse = '" + s1 + "'";
                
                    SQLiteDataReader reader = cmd.ExecuteReader();
                    if (reader.HasRows)
                   {
                        MessageBox.Show("esiste già un altro elemento con lo stesso codice sottoclasse " + s1);
                        return;
                    }
                    conn.Close();
                }
            }
      
            using (SQLiteConnection conn = SetConnection())
            {
                conn.Open();
                using (SQLiteCommand cmd = new SQLiteCommand(conn))
                {

                   string s2 = s1.Substring(0, 1);

                   string s4 = "insert into linea values('" + s1 + "','" + s2 + "','" + s5 + "','" + s6 + "','" + s3 + "','" + ss + "')";
  //              MessageBox.Show(s4);
    
                   cmd.CommandText = s4;

                   cmd.ExecuteNonQuery();
                }
                conn.Close();
            }

            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.No; 
            this.Close();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            ColorDialog colorDialog1 = new ColorDialog();
            if (colorDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox3.BackColor = colorDialog1.Color;
            }
        }

         private void textBox1_Enter1(object sender, EventArgs e)
        {
            string s1;
            string ss = comboBox1.Text;
             if (ss.Length > 0)
            {
                s1 = ss.Substring(0, 1);
                s1 = string.Concat(s1, "L");
                
            }
            else {
                s1 = "1L"; 
            }
            textBox1.Text = s1; 
        }
/*
         private void listBox1_SelectedValueChanged(object sender, EventArgs e)
         {
             string s1;
             string ss = listBox1.Text;
             MessageBox.Show(ss);
             if (ss.Length > 0)
             {
                 s1 = ss.Substring(0, 1);
                 s1 = string.Concat(s1, "L");

             }
             else
             {
                 s1 = "1P";
             }
             textBox1.Text = s1; 
         }

         private void listBox1_MouseEnter(object sender, EventArgs e)
         {
             Size sz = listBox1.Size;
             sz.Height = 140;
             listBox1.Size = sz;
             textBox1.Visible = false;
             textBox4.Visible = false;
             textBox5.Visible = false;
         }

         private void listBox1_MouseLeave(object sender, EventArgs e)
         {
             Size sz = listBox1.Size;
             sz.Height = 20;
             listBox1.Size = sz;
             textBox1.Visible = true;
             textBox4.Visible = true;
             textBox5.Visible = true;
         }

 */



         private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
         {
             int index = comboBox1.SelectedIndex + 1;
             string ss = index.ToString() + "L";
             textBox1.Text = ss;
         }

    }
}
